package mbsl.util;

import java.util.Comparator;

/** 
 * A Comparator<String> which ignores case.
 * <p>
 * The algorithm compares the strings char by char, converting them to lower case using Character.toLowerCase, and
 * returning the difference (if there is one). This will not be accurate for non-English alphabets, who may
 * prefer to use the more simplistic<br>
 * {@code return string1.toLower().compareTo(string2.toLower())}
 */
public class CaselessStringComparator implements Comparator<String> {
	@Override
	public int compare(String pString1, String pString2) {
		int length1 = pString1.length();
		int length2 = pString2.length();
		
		for (int i = 0; i < length1; i++) {
			if (i >= length2)
				return 1;
			
			int char1 = Character.toLowerCase(pString1.charAt(i));
			int char2 = Character.toLowerCase(pString2.charAt(i));
			
			if (char1 != char2)
				return char1 - char2;
		}
		
		return -1;
	}
}
